package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.bean.BaseMenu;
import com.hhs.storage.bean.BaseMenuRole;
import com.hhs.storage.bean.BaseMenuUser;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.dao.BaseMenuDao;
import com.hhs.storage.service.BaseMenuRoleService;
import com.hhs.storage.service.BaseMenuService;
import com.hhs.storage.service.BaseMenuUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@Service
@Slf4j
@Transactional
public class BaseMenuServiceImpl extends ServiceImpl<BaseMenuDao, BaseMenu> implements BaseMenuService {

    @Resource
    BaseMenuRoleService jxcMenuRoleService;
    @Resource
    BaseMenuUserService baseMenuUserService;

    @Override
    public List<BaseMenu> findByUserId(String userId){
        return this.baseMapper.findByUserId(userId);
    }

    @Override
    public Map<String, Object> dele( String id) {
        if(StringUtils.isEmpty(id)){
            return ResponseInfo.error("数据错误");
        }
        int count = count(new QueryWrapper<BaseMenu>().eq("pid_", id));
        if(count>0){
            return ResponseInfo.error("该菜单有下级菜单，无法删除");
        }
        //先删除关联表
        jxcMenuRoleService.remove(new QueryWrapper<BaseMenuRole>().eq("menu_id_", id));
        baseMenuUserService.remove(new QueryWrapper<BaseMenuUser>().eq("menu_id_", id));
        //再删除菜单数据
        remove(new QueryWrapper<BaseMenu>().eq("id_", id));
        return ResponseInfo.success(null);

    }

    @Override
    public List<BaseMenu> findByRoleId(String roleId) {
        return this.baseMapper.findByRoleId(roleId);
    }
}
